import {
    Button,
    Form,
    Segment
} from "semantic-ui-react";
import PubSub from "pubsub-js";
import React, { Component } from "react";
import WalletTools from '../servises/service';


class MnemonicTab extends Component {

    state = {
        mmic:'',
        path:"m/44'/60'/0'/0/0",
        wallets:[],
    }

    handleChange = (e,{name,value})=>{
        this.setState({[name]:value})
    }

    handleGenMicc =()=>{
        let words = WalletTools.createMnemonic()

        this.setState({mmic:words})
    }

    onMMICClick = ()=>{
        let {mmic,path} = this.state

        let wallets = WalletTools.newWalletFromMmic(mmic,path)
        if (wallets){
            this.setState({wallets},()=>{
                PubSub.publish("onLoginSuccessfully", this.state.wallets);
                console.log(this.state.wallets)
            })
        }else {

            alert("钱包生成失败，请检查导入助记词是否正确！")
        }



    }


    render() {
        return (
            <Form size="large">
                <Segment stacked>
                    <Form.TextArea
                        placeholder="12 words"
                        name="mmic"
                        value={this.state.mmic}
                        onChange={this.handleChange}
                    />
                    <Form.Input
                        fluid
                        icon="user"
                        iconPosition="left"
                        name="path"
                        value={this.state.path}
                        onChange={this.handleChange}
                    />
                    <Button onClick={this.handleGenMicc}>随机生成</Button>
                    <br />
                    <br />

                    <Form.Button onClick = {this.onMMICClick} color="teal" fluid size="large">
                        助记词导入(下一步)
                    </Form.Button>
                </Segment>
            </Form>
        );
    }
}

export default MnemonicTab